﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Control.Dal;

namespace Control.Bus
{
    public class BusExportBillDetail
    {
        public int ID { get; set; }
        private BusExportBill bill;
        public BusExportBill Bill
        {
            set { bill = value; }
            get
            {
                if (bill == null)
                    bill = new BusExportBill();
                return bill;
            }
        }
        public string MaBO
        {
            get { return bill.BillCode; }
        }
        public string Date
        {
            get { return bill.Date.ToShortDateString(); }
        }
        public int Quantity { get; set; }
        public int RealQuantity { get; set; }
        public int Recieve { get; set; }
        public int ChenhLech
        {
            get { return RealQuantity - Quantity; }
        }
        public int HaoHut
        {
            get { return RealQuantity - Quantity - Recieve; }
        }
        private BusProduct prod;
        public BusProduct Prod
        {
            set { prod = value; }
            get
            {
                if (prod == null)
                    prod = new BusProduct();
                return prod;
            }
        }
        public string KeProd
        {
            get { return Prod.ShelfName; }
        }
        public string MaProd
        {
            get { return Prod.ProductCode; }
        }
        private bool isSaved;
        public bool IsSaved
        {
            get
            {
                if (isSaved == null)
                    isSaved = false;
                return isSaved;
            }
            set { isSaved = value; }
        }

        public void Insert()
        {
            ID =  DalExportBillDetail.Insert(this);
            BusWareHouse Wh = BusWareHouse.GetWHByDateAndProd(Bill.Date, Prod);

            try
            {
                Wh.Export += (RealQuantity-Recieve);
                Wh.Store -= (RealQuantity - Recieve);
                Wh.UpDate();
            }
            catch
            {
                DateTime billdate = this.Bill.Date;
                Wh.Date = billdate;
                Wh.Export = RealQuantity-Recieve;
                Wh.Store = Prod.GetTonKho(billdate) - (RealQuantity-Recieve);
                Wh.Prod = Prod;
                Wh.Insert();
            }

        }

        public void Delete()
        {
            DalExportBillDetail.Delete(this);
            BusWareHouse Wh = BusWareHouse.GetWHByDateAndProd(Bill.Date, Prod);

            try
            {
                Wh.Export -= (RealQuantity - Recieve);
                Wh.Store += (RealQuantity - Recieve);
                Wh.UpDate();
            }
            catch
            {

            }
        }

        public void Update()
        {
            BusExportBillDetail bd = DalExportBillDetail.GetByID(this.ID);
            DalExportBillDetail.Update(this);
            BusWareHouse Wh = BusWareHouse.GetWHByDateAndProd(Bill.Date, Prod);

            try
            {
                Wh.Export -= ((bd.RealQuantity-bd.Recieve) - (RealQuantity - Recieve));
                Wh.Store += ((bd.RealQuantity - bd.Recieve) - (RealQuantity - Recieve));
                Wh.UpDate();
            }
            catch
            {

            }
        }

        public static BusExportBillDetail GetByID(int p)
        {
            return DalExportBillDetail.GetByID(p);
        }



        public static List<BusExportBillDetail> Find(string MaBO, string TenBO, string MaProd, DateTime fromDate, DateTime toDate)
        {
            DateTime newfromDate = new DateTime((int)fromDate.Year, (int)fromDate.Month, (int)fromDate.Day, 0, 0, 1);
            DateTime newtoDate = new DateTime((int)toDate.Year, (int)toDate.Month, (int)toDate.Day, 23, 59, 59);
            return DalExportBillDetail.Find( MaBO,TenBO, MaProd,newfromDate,newtoDate);
        }
    }
}
